//$.noConflict();
$(document).ready(function() {

	var currentSection = $("#currentSection").val();
	if (currentSection == "blog") {
		
	}

	// Política de Reservas
	$("a#reservationPolicyLink").fancybox({
		'autoDimensions': false,
		'width': '800',
		'height': '500',
		'centerOnScroll': true
	});

	// Estilos del menú
	$("div.menuoption").hover(
		function() {$(this).addClass("menuoption-hover");},
		function() {$(this).removeClass("menuoption-hover");}
	);

	$.fn.fancybox.defaults.transitionIn = 'fade';
	$.fn.fancybox.defaults.transitionOut = 'fade';

	// Enviar a un amigo
	$("#sendtofriendLink").fancybox({
		'scrolling': 'no',
		'titleShow': false
	});
	$("a[rel=gallery]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Galería de fotos</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=sarria]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Sarriá</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=portomarin]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Portomarín</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=palasderei]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Palas de Rei</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=arzua]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Arzúa</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=pedrouzo]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Pedrouzo</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a[rel=santiago]").fancybox({
		'centerOnScroll': true,
		'titlePosition': 'inside',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<p><b>Alojamientos en Santiago de Compostela</b> | ' + (currentIndex + 1) + '/' + currentArray.length + '</p>';
		}
	});
	$("a#commentPicture").fancybox();
	$("#sendtofriendForm").bind("submit", function() {
		$.fancybox.showActivity();
		var to = $.trim($("#sendtofriendTo").val());
		var from = $.trim($("#sendtofriendFrom").val());
		var message = $.trim($("#sendtofriendMessage").val());
		//var url = $("#pageurl").val();
		var url = window.location.href;
		$.post($("#sendtofriendAction").val(),
			{to: to, from: from, message: message, url: url},
			function(data) {
				var result = data.substr(1,2);
				var message = data.substr(4);
				if (result == "ko") {
					$("#sendtofriendError").html(message);
					$("#sendtofriendError").show();
					$.fancybox.resize();
					$.fancybox.hideActivity();
				} else {
					$("#sendtofriendError").hide();
					$("#sendtofriendTo").val("");
					$("#sendtofriendFrom").val("");
					$("#sendtofriendMessage").val("");
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', false);
				}
			}
		);
		return false;
	});
	// Elección del camino en el formulario de reservas
	$("#listOfTours").change(function() {
		var tourId = $("#listOfTours").val();
		$("#tourId").val(tourId);
		if (tourId == "") {
			$("#tour6StartDateLayer").hide();
			$("#tour7StartDateLayer").hide();
			$("#genericStartDateLayer").hide();
		} else if (tourId == "6") {
			$("#tour6StartDateLayer").show();
			$("#tour7StartDateLayer").hide();
			$("#genericStartDateLayer").hide();
		} else if (tourId == "7") {
			$("#tour6StartDateLayer").hide();
			$("#tour7StartDateLayer").show();
			$("#genericStartDateLayer").hide();
		} else {
			$("#tour6StartDateLayer").hide();
			$("#tour7StartDateLayer").hide();
			$("#genericStartDateLayer").show();
		}
	});
	$("#newCommentForm").submit(function() {
		var options = {
			success: function(responseText, statusText) {
				showFancybox('<div class="fancyboxNotifications">Tu comentario ha sido añadido correctamente, pero tiene que ser revisado antes de que aparezca publicado. Este proceso puede tardar hasta 24 horas.</div>', true);
				/*
				var result = responseText.substr(0,2);
				var message = responseText.substr(3);
				if (result == "ok") {
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', true);
				} else if (result == "ko") {
					showFancybox('<div class="fancyboxErrors">'+message+'</div>', false);
				} else {
					showFancybox('<div class="fancyboxErrors">Error desconocido</div>', false);
				}
				*/
			}
		};
		$(this).ajaxSubmit(options);
		return false;
	});
	$("#newCommentSubmit2").click(function() {
		// Recogemos los valores del formulario
		var blogId = $("#blogId").val();
		var entryId = $("#entryId").val();
		var name = $.trim($("#name").val());
		var comment = $.trim($("#comment").val());
		var captcha = $.trim($("#captcha").val());
		// Llamamos al script
		$.post($("#action").val(),
			{blogId: blogId, entryId: entryId, name: name, comment: comment, captcha: captcha},
			function(data) {
				var result = data.substr(1,2);
				var message = data.substr(4);
				if (result == "ko") {
					showFancybox('<div class="fancyboxErrors">'+message+'</div>', false);
					return false;
				} else {
					// Vaciamos el contenido de los campos del formulario y actualizamos el captcha
					/*$("#name").val("");
					$("#comment").val("");
					$("#captcha").val("");
					regenerateCaptcha();*/
					// Mostramos un mensaje
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', true);
					return false;
				}
			}
		);
	});
	// Envío del formulario de contacto
	$("#contactSubmit").click(function() {
		// Recogemos los valores del formulario
		var name = $.trim($("#name").val());
		var email = $.trim($("#email").val());
		var email2 = $.trim($("#email2").val());
		var subject = $.trim($("#subject").val());
		var message = $.trim($("#message").val());
		var captcha = $.trim($("#captcha").val());
		// Llamamos al script
		$.post($("#action").val(),
			{name: name, email: email, email2: email2, subject: subject, message: message, captcha: captcha},
			function(data) {
				var result = data.substr(1,2);
				var message = data.substr(4);
				if (result == "ko") {
					showFancybox('<div class="fancyboxErrors">'+message+'</div>', false);
					return false;
				} else {
					// Mostramos un mensaje
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', true);
					return false;
				}
			}
		);
	});
	// Envío del formulario de reservas
	$("#reservationsSubmit").click(function() {
		// Recogemos los valores del formulario
		var accept = $("#accept").attr("checked");
		if (accept != true) {
			showFancybox('<div class="fancyboxErrors">Debes aceptar la Política de Reservas.</div>', false);
			return false;
		}
		var tourId = $("#tourId").val();
		var tour6StartDate = $("#tour6StartDate").val();
		var tour7StartDate = $("#tour7StartDate").val();
		var genericStartDay = $("#genericStartDay").val();
		var genericStartMonth = $("#genericStartMonth").val();
		var name = $.trim($("#name").val());
		var lastName = $.trim($("#lastName").val());
		var nif = $.trim($("#nif").val());
		var age = $.trim($("#age").val());
		var email = $.trim($("#email").val());
		var email2 = $.trim($("#email2").val());
		var address = $.trim($("#address").val());
		var town = $.trim($("#town").val());
		var zipCode = $.trim($("#zipCode").val());
		var province = $.trim($("#province").val());
		var country = $.trim($("#country").val());
		var phone1 = $.trim($("#phone1").val());
		var phone2 = $.trim($("#phone2").val());
		var observations = $.trim($("#observations").val());
		var captcha = $.trim($("#captcha").val());
		// Acompañantes
		var numCompanions = 0;
		var companions = "";
		var maxCompanions = $("#maxCompanions").val();
		for (i=1; i<=maxCompanions; i++) {
			var id = "companionBox"+i;
			var visible = $("#"+id).is(":visible");
			if (visible == true) {
				numCompanions++;
				var cName = $.trim($("#companionName"+i).val());
				var cLastName = $.trim($("#companionLastName"+i).val());
				var cNif = $.trim($("#companionNif"+i).val());
				var cAge = $.trim($("#companionAge"+i).val());
				companions += cName + "#" + cLastName + "#" + cNif + "#" + cAge + "|";
			}
		}
		// Llamamos al script
		$.post($("#action").val(),
			{tourId: tourId, tour6StartDate: tour6StartDate, tour7StartDate: tour7StartDate, genericStartDay: genericStartDay, genericStartMonth: genericStartMonth, name: name, lastName: lastName, nif: nif, age: age, email: email, email2: email2, address: address, town: town, zipCode: zipCode, province: province, country: country, phone1: phone1, phone2: phone2, observations: observations, captcha: captcha, numCompanions: numCompanions, companions: companions},
			function(data) {
				var result = data.substr(1,2);
				var message = data.substr(4);
				if (result == "ko") {
					showFancybox('<div class="fancyboxErrors">'+message+'</div>', false);
					return false;
				} else {
					// Vaciamos el contenido de los campos del formulario y actualizamos el captcha
					/*$("#name").val("");
					$("#lastName").val("");
					$("#nif").val("");
					$("#age").val("");
					$("#email").val("");
					$("#address").val("");
					$("#town").val("");
					$("#zipCode").val("");
					$("#province").val("");
					$("#country").val("");
					$("#phone1").val("");
					$("#phone2").val("");
					$("#observations").val("");
					$("#captcha").val("");*/
					// Acompañantes
					for (i=1; i<=maxCompanions; i++) {
						$("#companionName"+i).val("");
						$("#companionLastName"+i).val("");
						$("#companionNif"+i).val("");
						$("#companionAge"+i).val("");
						$("#companionBox"+i).hide();
					}
					// Regeneramos el captcha
					regenerateCaptcha();
					// Mostramos un mensaje
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', true);
					return false;
				}
			}
		);
	});
	// Fancybox para la galería de fotos
	$("a.gallery").fancybox(
		{'titleShow': false}
	);
});
function showFancybox(text, reload) {
	$.fancybox(
		text,
		{
			'autoDimensions': false,
			'width': 500,
			'height': 'auto',
			'onClosed': function() {
				if (reload == true) {
					var href = window.location.href;
					window.location.href = href;
				}
			}
		}
	);
}
function showCompanion() {
	var maxCompanions = $("#maxCompanions").val();
	for (i=1; i<=maxCompanions; i++) {
		var id = "companionBox"+i;
		var visible = $("#"+id).is(":visible");
		if (visible == false) {
			$("#"+id).show();
			break;
		}
	}
	$("#companionBoxesDivider").show();
}
function hideCompanion() {
	var maxCompanions = $("#maxCompanions").val();
	for (i=maxCompanions; i>=1; i--) {
		var id = "companionBox"+i;
		var visible = $("#"+id).is(":visible");
		if (visible == true) {
			$("#"+id).hide();
			if (i == 1) {
				$("#companionBoxesDivider").hide();
			}
			break;
		}
	}
}

function showFaqAnswer(id) {
	var visible = $("#faqAnswer"+id).is(":visible");
	if (visible == true) {
		$("#faqAnswer"+id).fadeOut();
	} else {
		$("#faqAnswer"+id).fadeIn();
	}
}
